import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Sleeper;
import org.openqa.selenium.support.ui.WebDriverWait;
import sun.reflect.misc.FieldUtil;

import javax.swing.*;
import javax.xml.bind.Element;

import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException, IOException {
        //test01();
        //test02();
        //test03();
        //test04();
        //test05();
        //test06();
        //test07();
        //test08();
        //test9();
        test10();
    }

    private static void test10() throws InterruptedException, IOException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file, new File("D://20231019jietu.png"));
    }

    private static void test9() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);
        // 通过getWindowHandles获取所有的窗口句柄
        // 通过getWindowHandle获取的get打开的页面窗口句柄
        System.out.println(webDriver.getWindowHandle());
        Set<String> handles = webDriver.getWindowHandles();
        String target_handle = "";
        for(String handle:handles) {
            target_handle = handle;
        }
        webDriver.switchTo().window(target_handle);
        sleep(3000);
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();

    }

    private static void test08() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(4000);
        //全部关闭
//        webDriver.quit();
        // 只关闭旧的标签页
        webDriver.close();
    }

    private static void test07() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        //打开百度
        webDriver.get("https://www.baidu.com");
        //搜索521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        // 找到图片按钮
        WebElement element = webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));
        // 鼠标右击
        Actions actions = new Actions(webDriver);
        sleep(3000);
        actions.moveToElement(element).contextClick().perform();

    }

    private static void test06() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        //打开百度
        webDriver.get("https://www.baidu.com");
        //搜索521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        sleep(3000);
        //control + A
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "A");
        sleep(3000);
        //control + X
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "X");
        sleep(3000);
        //control + V
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "V");
    }

    private static void test05() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        //打开百度
        webDriver.get("https://www.baidu.com");
        //搜索521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        webDriver.findElement(By.cssSelector("#su")).click();
        // 强制等待3秒
        sleep(3000);
        // 浏览器后退
        webDriver.navigate().back();
        // 强制等待3秒
        sleep(3000);
        // 刷新
        webDriver.navigate().refresh();
        // 强制等待3秒
        sleep(3000);
        // 浏览器前进
        webDriver.navigate().forward();
        sleep(3000);
        //滚动条
        ((JavascriptExecutor) webDriver).executeScript("document.documentElement.scrollTop=10000");
        sleep(2000);
        //最大化
        webDriver.manage().window().maximize();
        sleep(2000);
        // 全屏
        webDriver.manage().window().fullscreen();
        sleep(2000);
        // 设置像素
        webDriver.manage().window().setSize(new Dimension(600,1000));
    }

    private static void test04() {
        WebDriver webDriver = new ChromeDriver();
        //打开百度
        webDriver.get("https://www.baidu.com");
        WebDriverWait wait = new WebDriverWait(webDriver, 1);
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bottom_layer > div > p:nth-child(9) > span")));
        wait.until(ExpectedConditions.titleIs("百度一下，你就知道"));
    }

    private static void test03() {
        WebDriver webDriver = new ChromeDriver();
        //打开百度
        webDriver.get("https://www.baidu.com");
        String url = webDriver.getCurrentUrl();
        String title = webDriver.getTitle();
        if(url.equals("https://www.baidu.com/") && title.equals("百度一下，你就知道")) {
            System.out.println("当前页面url:" + url + "，当前页面title:" + title);
            System.out.println("测试通过");
        } else {
            System.out.println("测试不通过");
        }
    }


    private static void test02() {
        WebDriver webDriver = new ChromeDriver();
        //打开百度
        webDriver.get("https://www.baidu.com");
        // 找到百度搜索输入框，输入“软件测试”
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        // 点击了百度一些按钮
        webDriver.findElement(By.cssSelector("#su")).click();
        // 清空百度搜索输入框中的数据
        webDriver.findElement(By.cssSelector("#kw")).clear();

    }

    private static void test01() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        //打开百度
        webDriver.get("https://www.baidu.com");
        //找到百度搜索框
        // CSS选择器定位元素
        //WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));
        // 通过XPath定位元素
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        // 输入软件测试
        element.sendKeys("软件测试");
        // 找到百度一下按钮
        // 点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        // 校验
        // 找到搜索结果
        List<WebElement> elements =  webDriver.findElements(By.cssSelector("a em"));
        int flg = 0;
        for(int i = 0; i < elements.size(); i++) {
            // 如果返回的结果有软件测试，证明测试通过，否则测试不通过
            if(elements.get(i).getText().equals("软件测试")) {
                flg = 1;
                System.out.println("测试通过");
                break;
            }
        }
        if(flg == 0) {
            System.out.println("测试不通过");
        }
    }


}
